import request from '@/utils/request'
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { getCurrentTime } from '@/utils'

const pref = '/sysQueryMain'

export const getPage = (param) => {
  return request.post(pref + '/getPage', param)
}

export const download = (param) => {
  return downloadExcel(param, {});
}

export function downloadExcel(params, headers) {
  headers['x-token'] = getToken();
  axios.post('/api/' + 'anon'  + pref ,
    {
      params: params,
      headers: headers,
      responseType: 'blob'
    }).then(res => {
      const blob = new Blob([res.data])
      const url = window.URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = getCurrentTime() + '.xlsx'
      a.click()
      window.URL.revokeObjectURL(url)
    })
}

export const queryPage = (param) => {
  return request.post(pref + '/queryPage', param)
}

export const getList = (param) => {
  return request.post(pref + '/getList', param)
}

export const deleteById = (id) => {
  return request.delete(pref + '?id=' + id)
}

export const save = (param) => {
  return request.post(pref, param)
}

export const getOne = (id) => {
  return request.get(pref + '?id=' + id)
}
